using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Common;
using Common.Enum;
using SExperiment;

namespace MainView.Windows
{
	public partial class SubsetGenoRenameWindow : Window, IComponentConnector
	{
		public Subset Subset { get; set; }

		public bool IsDecrypt { get; set; }

		public List<KeyValuePair> itemSource { get; set; }

		public SubsetGenoRenameWindow()
		{
			InitializeComponent();
		}

		public void SetDataResource()
		{
			if (Subset.GenoTypeList.Count > 3)
			{
				itemSource = new List<KeyValuePair>();
				for (int i = 3; i < Subset.GenoTypeList.Count; i++)
				{
					KeyValuePair keyValuePair = new KeyValuePair();
					keyValuePair.Key = Subset.GenoTypeList.ElementAt(i).Key;
					keyValuePair.Value = Subset.GenoTypeList.ElementAt(i).Value;
					itemSource.Add(keyValuePair);
				}
				myGrideEx.ItemsSource = itemSource;
			}
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			if (itemSource.Select((KeyValuePair s) => s.Value).Distinct().Count() == 6 && itemSource.Where((KeyValuePair s) => s.Value.ToString().Trim().Equals(string.Empty)).Count() == 0)
			{
				foreach (KeyValuePair temp in itemSource)
				{
					List<KeyValuePair> genoTypeList = Subset.GenoTypeList;
					Func<KeyValuePair, bool> predicate = (KeyValuePair s) => (EGenoType)s.Key == (EGenoType)temp.Key;
					KeyValuePair keyValuePair = genoTypeList.Where(predicate).FirstOrDefault();
					if (keyValuePair != null)
					{
						keyValuePair.Value = temp.Value;
					}
				}
				base.DialogResult = true;
				Close();
			}
			else
			{
				int num = itemSource.Where((KeyValuePair s) => s.Value.ToString().Trim().Equals(string.Empty)).Count();
				string messageBoxText = ((num > 0) ? Utility.GetMessage("msg_genoname_is_required") : Utility.GetMessage("msg_genoname_is_repeat"));
				MessageBox.Show(messageBoxText);
			}
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void myGrideEx_LoadingRow(object sender, DataGridRowEventArgs e)
		{
			KeyValuePair keyValuePair = (KeyValuePair)e.Row.DataContext;
			e.Row.Width = 300.0;
			StringBuilder stringBuilder = new StringBuilder(keyValuePair.Key.ToString());
			stringBuilder.Insert(4, "_");
			string message = Utility.GetMessage(stringBuilder.ToString());
			e.Row.Header = message;
		}

		private void myGrideEx_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			e.Cancel = !IsDecrypt;
		}
	}
}
